#include <iostream>
#include <vector>
#include <string>

struct Student {
    std::string name;
    int age;


    Student(std::string&& n, int a)
        :name(std::move(n)), age(a)
    {
        std::cout << "构造 " << this << std::endl;
    }

    Student(const Student& s)
        : name(std::move(s.name)), age(s.age)
    {
        std::cout << "拷贝构造 " << this << " from " << &s << std::endl;
    }

    Student(Student&& s)
        :name(std::move(s.name)), age(s.age)
    {
        std::cout << "移动构造 " << this << " from " << &s << std::endl;
    }

    Student& operator=(const Student& s);

    ~Student()
    {
        std::cout << "析构 " << this << std::endl;
    }
};

int main()
{
    std::vector<Student> classes_one;
    std::vector<Student> classes_two;
    Student *p = new Student("!!!", 30);

    std::cout << "emplace_back:" << std::endl << "const: " << std::endl;
    classes_one.emplace_back("xiaohong", 24);
    std::cout << "std::move : " << std::endl;
    classes_one.emplace_back(std::move(*p));
    std::cout << "new p:" << std::endl;
    p = new Student("!!!", 30);
    std::cout << "insert:" << std::endl;
    classes_one.emplace_back(*p);
    std::cout << "clean all in vector" << std::endl;
    classes_one.clear();
    std::cout << "emplace_back end" << std::endl;

    std::cout << "push_back:" << std::endl;
    classes_two.push_back(Student("xiaoming", 23));
    std::cout << "std::move : " << std::endl;
    classes_two.push_back(std::move(*p));
    std::cout << "new p:" << std::endl;
    p = new Student("!!!", 30);
    std::cout << "insert:" << std::endl;
    classes_two.push_back(*p);
    std::cout << "clean all in vector" << std::endl;
    classes_two.clear();
    std::cout << "push_back end" << std::endl;

    std::cout << "step by step" << std::endl;
    p = new Student("!!!", 30);

    std::cout << "emplace_back:" << std::endl;
    std::cout << "std::move : " << std::endl;
    classes_one.emplace_back(std::move(*p));
    std::cout << "new p:" << std::endl;
    p = new Student("!!!", 30);
    std::cout << "insert:" << std::endl;
    classes_one.emplace_back(*p);
    std::cout << "clean all in vector" << std::endl;
    classes_one.clear();
    std::cout << "emplace_back end" << std::endl;

    std::cout << "push_back:" << std::endl;
    std::cout << "std::move : " << std::endl;
    classes_two.push_back(std::move(*p));
    std::cout << "new p:" << std::endl;
    p = new Student("!!!", 30);
    std::cout << "insert:" << std::endl;
    classes_two.push_back(*p);
    std::cout << "clean all in vector" << std::endl;
    classes_two.clear();
    std::cout << "push_back end" << std::endl;
    return 0;
}

/*
作者：我的倩宝
链接：https://www.jianshu.com/p/c0cbd9dbf772
来源：简书
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
*/